<?php
/**
 * 文件上传类 Controller
 * User: crstu
 * Date: 2016/5/28
 * Time: 16:48
 */

namespace backend\controllers;

use yii;
use backend\commands\BaseAdminController;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;
use Qiniu\Auth as QiniuAuth;
use Qiniu\Storage\UploadManager;
use yii\helpers\Json;

class UploadController extends BaseAdminController
{

    /**
     * 上传图片
     */
    public function actionUpImage(){
        $tmpFile   = UploadedFile::getInstanceByName('upImage');//读取图像上传域,并使用系统上传组件上传
        if(!empty($tmpFile)){
            $fileExt   = $tmpFile->getExtension();
            $fileSize  = $tmpFile->size;
            if($fileSize <= 2048000){
                $fileName  = time() . rand(100000, 999999) . '.' . $fileExt;
                $fileUrl   = '/' . date('Y') . '/' . date('m') . '/' . date('d') .'/';
                $filePath  = __DIR__ . '/../../images/' . $fileUrl;
                FileHelper::createDirectory($filePath);
                $tmpFile->saveAs($filePath . $fileName);
                $this->renderJson(['status'=>1, 'url'=>('http://' . \Yii::$app->params['images_domain'] . $fileUrl.$fileName)]);
            }

        }
    }


    public function actionQiniu(){

        $accessKey = Yii::$app->params['qiniu_access_key'];
        $secretKey = Yii::$app->params['qiniu_secret_key'];
        $auth      = new QiniuAuth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = Yii::$app->params['qiniu_images_space'];

        // 生成上传 Token
        $token = $auth->uploadToken($bucket);


        $tmpFile = UploadedFile::getInstanceByName('upImage');

        $fileName = time() . rand(100000, 999999) . '.' . $tmpFile->getExtension();

        // 要上传文件的本地路径
        $filePath = $tmpFile->tempName;

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath);
        if ($err !== null) {
            $this->renderJson(0, $err, '上传失败！');
        } else {
            $fileUrl = 'http://' . Yii::$app->params['images_domain'] . '/' . $ret['key'];
            $this->renderJson(1, $fileUrl, '上传成功！');
        }
    }

    public function actionQiniuEditor(){

        $accessKey = Yii::$app->params['qiniu_access_key'];
        $secretKey = Yii::$app->params['qiniu_secret_key'];
        $auth      = new QiniuAuth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = Yii::$app->params['qiniu_images_space'];

        // 生成上传 Token
        $token = $auth->uploadToken($bucket);

        $tmpFile = UploadedFile::getInstanceByName('upload_file');

        $fileName = time() . rand(100000, 999999) . '.' . $tmpFile->getExtension();

        // 要上传文件的本地路径
        $filePath = $tmpFile->tempName;

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath);
        if ($err !== null) {
            return $this->renderJson(['status'=>false, 'msg'=>$err, 'file_path' => '']);
        } else {
            $fileUrl = 'http://' . Yii::$app->params['images_domain'] . '/' . $ret['key'];
            return $this->renderJson(["success"=>true, "file_path" => $fileUrl]);
        }
    }

}